[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.AddButtons.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.AddButtons.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using ScriptReferences;

    /// <summary>
    /// This control keeps shows appropriate buttons for the currently active item of the MForm tree
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Resources.js.jquery.jquery.js")]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.AddButtons.js")]
    public class AddButtons : WebControl, IScriptControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AddButtons"/> class.
        /// </summary>
        public AddButtons()
        {
            Depth = 2;
            CountEmptyContainers = true;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to count empty containers.
        /// </summary>
        [DefaultValue(true)]
        public bool CountEmptyContainers
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the depth to which add buttons should be displayed for item parents.
        /// </summary>
        [DefaultValue(2)]
        public int Depth
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the <see cref="T:System.Web.UI.HtmlTextWriterTag"/> value that corresponds to this Web server control. This property is used primarily by control developers.
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// Gets a collection of script descriptors that represent ECMAScript (JavaScript) client components.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerable"/> collection of <see cref="T:System.Web.UI.ScriptDescriptor"/> objects.
        /// </returns>
        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            var descriptor = new ScriptControlDescriptor("BM.AddButtons", ClientID);
            descriptor.AddProperty("countEmptyContainers", CountEmptyContainers);
            descriptor.AddProperty("depth", Depth);
            yield return descriptor;
        }

        /// <summary>
        /// Gets a collection of <see cref="T:System.Web.UI.ScriptReference"/> objects that define script resources that the control requires.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerable"/> collection of <see cref="T:System.Web.UI.ScriptReference"/> objects.
        /// </returns>
        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            return null;
        }

        /// <summary>
        /// registers script control and script descriptors
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            var scriptManager = ScriptManager.GetCurrent(Page);
            if (scriptManager == null)
            {
                return;
            }

            scriptManager.RegisterScriptControl(this);
            scriptManager.RegisterScriptDescriptors(this);
            ScriptIncluder.IncludeResources(this);
        }
    }
}